#include "ComboProgress.h"
#include "PlayLayer.h"
#include "AudioManage.h"

static const char* CONST_PNG_PROGRESSBACK = "Progress/progress_back.png";
static const char* CONST_PNG_PROGRESSBAR = "Progress/progress.png";

ComboProgress::ComboProgress()
{
	this->create();
	this->initWithFile(CONST_PNG_PROGRESSBACK);
	this->setScale(0.5f);

	float width = this->getContentSize().width;
	float height = this->getContentSize().height;

	Sprite* progressBar = Sprite::create(CONST_PNG_PROGRESSBAR);
	progressBar->setScale(0.5f);
	//progressBar->setPosition(Vec2(width / 2, height / 2));

	ComboProgress::timeProgress = ProgressTimer::create(progressBar);
	timeProgress->setType(ProgressTimer::Type::BAR);

	this->addChild(timeProgress);
}

ComboProgress::~ComboProgress()
{
}

ComboProgress* ComboProgress::thisSprite = nullptr;
ProgressTimer* ComboProgress::timeProgress;
int ComboProgress::ComboCount = 0;

ComboProgress* ComboProgress::getInstance()
{
	if (nullptr == thisSprite)
	{
		thisSprite = new ComboProgress();
	}

	return thisSprite;
}

void ComboProgress::onEnter()
{
	float width = thisSprite->getContentSize().width;
	float height = thisSprite->getContentSize().height;

	timeProgress->setPosition(Vec2(width / 2, height / 2));
	timeProgress->setMidpoint(Point(0 , 0.5f));
	timeProgress->setBarChangeRate(Point(1, 0));

	timeProgress->setPercentage(100.0f);
}

void ComboProgress::addProgress()
{
	float per = timeProgress->getPercentage();

	if (per < 100.0f)
	{
		per += 2;		
		timeProgress->setPercentage(per);
	}
	if (ComboCount > 0)
	{
		AudioManage::comboSound();
		PlayLayer::addTime(ComboCount);
	}

	++ComboProgress::ComboCount;	
}

bool ComboProgress::subProgress()
{
	float per = timeProgress->getPercentage();

	if (per > 0)
	{
		per = per - 0.1f;
		timeProgress->setPercentage(per);			
	}
	else
	{
		return false;
	}

	if (per > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void ComboProgress::newProgress()
{
	timeProgress->setPercentage(100.0f);
	ComboProgress::ComboCount = 0;
}

void ComboProgress::disposeSprite()
{
	thisSprite = nullptr;
	ComboCount = 0;
}